package com.example.loginsystem.api;

import com.example.loginsystem.bean.Message;
import com.example.loginsystem.model.Paging;
import com.example.loginsystem.model.Result;
import com.example.loginsystem.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Cheung0
 * Created on 2021/11/24
 */
@RestController
@RequestMapping("/api/message")
public class MessageAPI {

    @Autowired
    private MessageService messageService;

    @PostMapping("/add")
    private Result<Message> save(@RequestBody Message message) {
        Result<Message> result = new Result();
        if (message.getContactInfo().length() > 128 || message.getDescription().length() > 128) {
            result.setResultFailed("描述或联系方式文字长度过长！");
            return result;
        }
        return messageService.add(message);
    }

    @DeleteMapping("/delete/{id}")
    private Result<Message> delete(@PathVariable("id") int id) {
        return messageService.delete(id);
    }

    @PostMapping("/update")
    private Result<Message> update(@RequestBody Message message) {
        return messageService.update(message);
    }

    @GetMapping("/getMessageByPage/{page}/{size}")
    public Paging<Message> getPageInfo(@PathVariable("page") int currentPage, @PathVariable("size") int pageSize) {
        Paging<Message> list = messageService.getByPage(currentPage, pageSize);
        return list;
    }

    @GetMapping("/getByMessageId/{id}")
    public Result<Message> getByMessagePusher(@PathVariable("id") int id) {
        return messageService.getById(id);
    }

    @GetMapping("/getByMessageUserId/{userId}")
    public Result<List<Message>> getByMessagePusher(@PathVariable("userId") String userId) {
        return messageService.getByUserId(userId);
    }

    @GetMapping("/getAll")
    public Result<List<Message>> getAll() {
        List<Message> messageList = new ArrayList<>();
        messageList = messageService.getAll();
        Result result = new Result();
        result.setResultSuccess("查询成功！", messageList);
        return result;
    }

}
